//
//  ServiceCenterViewController.m
//  TravelFollowYourHeart
//
//  Created by ENERGY on 2018/1/16.
//  Copyright © 2018年 Lwx. All rights reserved.
//

#import "ServiceCenterViewController.h"
#import "CRMediaPickerController.h"

@interface ServiceCenterViewController ()<CRMediaPickerControllerDelegate>
@property (nonatomic, strong)CRMediaPickerController *mediaPickerController;

@end

@implementation ServiceCenterViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    
    
     self.mediaPickerController = [[CRMediaPickerController alloc] init];
     self.mediaPickerController.delegate = self;
     self.mediaPickerController.mediaType = CRMediaPickerControllerMediaTypeVideo;
     self.mediaPickerController.sourceType = CRMediaPickerControllerSourceTypePhotoLibrary;
     self.mediaPickerController.allowsEditing = NO;
     self.mediaPickerController.cameraDevice = UIImagePickerControllerCameraDeviceRear;
     self.mediaPickerController.showsCameraControls = YES;
     [self.mediaPickerController show];
}


- (void)CRMediaPickerController:(CRMediaPickerController *)mediaPickerController didFinishPickingAsset:(ALAsset *)asset error:(NSError *)error
{
    
    if (!error)
    {
        if (asset)
        {
            if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypePhoto])
            {
                //图片
            }
            else if ([[asset valueForProperty:ALAssetPropertyType] isEqualToString:ALAssetTypeVideo])
            {
                //视频
                ALAssetRepresentation *rep = [asset defaultRepresentation];
                Byte *buffer = (Byte*)malloc((NSUInteger)rep.size);
                NSUInteger buffered = [rep getBytes:buffer fromOffset:0.0 length:(NSUInteger)rep.size error:nil];
                //                data = 视频数据
                NSData *data = [NSData dataWithBytesNoCopy:buffer length:buffered freeWhenDone:YES];
            }
        }
        else
        {
            NSLog(@"没选择");
        }
    }
    else
    {
        NSLog(@"%@", error.localizedDescription);
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
